<template>
	<view>
		<view class="cart-container" v-if="cart.length!==0">
			<my-address></my-address>
			<!-- 购物车商品列表区域 -->
			<view class="cart-title">
				<uni-icons type="shop" size="18"></uni-icons>
				<!-- 描述文本 -->
				<text class="cart-title-text">购物车</text>
			</view>

			<!-- 商品列表区域 -->
			<!-- 包裹性质窗口  滑动窗口删除-->
			<uni-swipe-action>
				<block v-for="(item,index) in cart" :key="index">
					<uni-swipe-action-item :right-options="options" @click="swipeActionClickHandler(item)">
						<my-goods :item="item" :showRadio="true" @radio-change="radioClickHandler" :show-num="true"
							@num-change="numberChangeHandler"></my-goods>
					</uni-swipe-action-item>
				</block>
			</uni-swipe-action>
			<!-- 结算模块 -->
			<view class="">
				<my-settle></my-settle>
			</view>
		</view>
		<view class="empty-cart" v-else>
			<image src="../../static/image/购物车为空.png" class="empty-img"></image>
			<text class="empty-text">空空如也</text>
		</view>
	</view>


</template>

<script>
	import {
		mapGetters,
		mapState,
		mapMutations
	} from 'vuex'
	import badgeMix from '@/mixins/tabbar-badge.js'


	export default {
		mixins: [badgeMix],
		data() {
			return {
				options: [{
					text: '取消',
					style: {
						backgroundColor: '#007aff'
					}
				}, {
					text: '删除',
					style: {
						backgroundColor: '#dd524d'
					}
				}]

			};
		},

		onShow() {
			this.setBadge()
		},
		computed: {

			...mapGetters('my_cart', ['total']),
			...mapState('my_cart', ['cart'])
		},
		watch: {
			total() {
				this.setBadge()
			}

		},
		methods: {
			...mapMutations('my_cart', ['updateGoodsState']),
			...mapMutations('my_cart', ['updateGoodsCount', 'removeById']),
			radioClickHandler(e) {
				//点击完之后更新
				this.updateGoodsState(e)

			},
			//商品数量发生变化
			numberChangeHandler(e) {
				this.updateGoodsCount(e)
			},
			setBadge() {
				// tabbar上面的徽标
				uni.setTabBarBadge({
					index: 2, //tabbar 索引
					text: this.total + '' //text值为字符串
				})
			},
			// 滑动删除
			swipeActionClickHandler(item) {
				this.removeById(item.goods_id)
			}
		}

	}
</script>

<style lang="scss">
	.cart-container {
		padding-bottom: 50px;
	}

	.cart-title {
		height: 40px;
		display: flex;
		align-items: center;
		padding-left: 5px;
		font-size: 14px;
		border-bottom: 1px solid #f5f5f5;

		.cart-title-text {
			margin-left: 10px;
		}

	}

	.empty-cart {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 150px;

		.empty-img {
			width: 100px;
			height: 100px;
		}

		.empty-text {
			font-size: 15px;
			color: gray;
			margin-top: 15px;
		}
	}
</style>